<?php
/**
 * Created by PhpStorm.
 * User: 子不语
 * Date: 2016/6/24
 * Time: 14:40
 */
global $db;
$sql='select id from score_log where opr_type=20 and uid='.$_SESSION['usersid'].' and to_days(now())=to_days(log_time)';
$res=$db->fetch_first($sql);
$id=$res['id'];
$point=get_rebate();

if($_GET['g']){
    $get=intval($_GET['g'])?1:0;
    if(!$id and $get and $point>0){

        $db->query('begin');
        //$db->query('SET AUTOCOMMIT=0');
        $sql='update users set `points`=`points`+'.$point.' where id='.$_SESSION['usersid'];
        if(!$db->query($sql)){
            $db->query('rollback');
            return result(1, '失败了,请联系管理员');
        }
        $info=get_info_point();
        $sql="UPDATE centerbank SET score = score-$point WHERE bankIdx = 9";
        $db->query($sql);
        $sql = "INSERT game_static (uid,typeid,points) values (" . $_SESSION['usersid'] . ",142, $point) ON DUPLICATE KEY UPDATE points=points+'$point'";
        $db->query($sql);
        $sql = "INSERT webtj (time,jjpoints) values (now(),$point) ON DUPLICATE KEY UPDATE jjpoints=jjpoints+'$point'";
        $db->query($sql);
        admin_log(20,$point,$info['points'],$info['back'],"每日返利",$_SESSION['usersid']);

        $db->query('commit');
        $db->query('SET AUTOCOMMIT=1');
        //RefreshPoints();
        RefreshPoints();
        return result(0, '领取成功');
    }else{
        return result(1,"您没有可以领取的返利" );
    }
    return result(1, "出错了");
}
function get_rebate()
{
    global $db;
    $sql = 'select sum(points) as point from game_day_static where uid=' . $_SESSION['usersid'] . ' and to_days(now())-to_days(time)=1';
    $res = $db->fetch_first($sql);
    $point = $res['point'];
	
	
	
    if ($point < 0) {
		
		/* wk  修改后台设置的回水率 */
		$hs_bl = 0;
		$sql = 'select hs_bl from web_config where id=1  ';
		$web_config = $db->fetch_first($sql);
		
		if($web_config['hs_bl']){
			$config = explode('|',$web_config['hs_bl']);
			
		 
			if(count($config) > 0){ 
				foreach($config as $k => $v){
					$a = explode('-',$v);
					if($a['0'] <= floor(abs($point)) && floor(abs($point)) >= $a['1']){
						$hs_bl = $a['2'];
						break;
					}	
				}
			}else{
				$point = 0;
			}	
		}else{
			$point = 0;
		}
		
        $point *= ($hs_bl/100);
    }else{
        return 0;
    }
    $point=floor(abs($point));
    return $point;
}



?>
    <div class='panel panel-default'>
    <div class='panel-heading'>领取返利</div>
    <div class='panel-body'>
    <table class='table table-striped table-hover table-bordered'>
    <tr>
    <td>可领取宝币</td>
    <td>领取状态</td>
    </tr>
    <tr>
    <td><?php echo $point;?></td>
    <td><?php if($point>0){?>
            <?php if($id){ echo '已经领取过了';}else{?>
            <a href="javascript:get();" class='btn btn-danger'>领取</a><?php }}?></td>
    </tr>

    </table>
    </div>
    </div>

    <div class='panel panel-default'>
    <div class='panel-heading'>领取记录</div>
    <div class='panel-body'>
    <table class='table table-striped table-hover table-bordered'>

    <tr>
    <td>领取时间</td>
    <td>领取金额</td>
    <td>领取状态</td>
    </tr>

          <?php
        $sql='select * from score_log where opr_type=20 and uid='.$_SESSION['usersid'].' order by id desc limit 30';
        $res=$db->query($sql);
        while ($row=$db->fetch_array($res)){
        ?>
    <tr>
    <td><?php echo $row['log_time'];?></td>
    <td><?php echo $row['amount'];?></td>
    <td>已领取</td>
    </tr>
        <?php }?>
    </table>
    </div>
    </div>
<script type="text/javascript">
    function get() {
        $.getJSON('smbinfo.php?act=rebate&g=1',function (data) {
            if(data.status==0){
                alert(data.message);
                getContent('smbinfo.php','rebate');
            }else{
                alert(data.message);
            }
        });

    }
</script>